/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.gatech.ealf.keyboardassistanceplaf;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;

import edu.gatech.ealf.EasyAccessibilityLookAndFeel;
import edu.gatech.ealf.GlassPaneManager;
import edu.gatech.ealf.ettplaf.ToolTipComponent;
import edu.gatech.ealf.ttsplaf.TTSHelper;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionListener;
import java.util.Timer;
import javax.swing.JComponent;

/**
 *
 * @author hemanthpai
 */
public class KAHelper {
    private ToolTipComponent typedCharacter;
    private TTSHelper helper;
    private Dimension defaultSize = new Dimension(20, 20);
    private Dimension characterSize;
    private Container glass;
    private Font originalFont;
    
    public HideCharacterDisplay hideDisplay = null;
    
    private JComponent curComponent;
    
    private boolean isDisplayOn;
    
    private long currentTimeStamp;
    
    public KAHelper(JComponent c){
        this.curComponent = c;
        hideDisplay = new HideCharacterDisplay();
    }

    public void keyTyped(char keyTyped){
        this.displayKeyTyped(keyTyped);
        this.speakKeyTyped(keyTyped);
    }
    
    public void displayKeyTyped(char keyTyped){
        if(KeyboardAssistanceLookAndFeel.getInstance().isDisplayEnabled()){
                hideDisplay();
        	showKeyDisplay(keyTyped);
        }
    }
    
    public void speakKeyTyped(char keyTyped){
        if(KeyboardAssistanceLookAndFeel.getInstance().isSpeechEnabled()){
            helper = TTSHelper.getInstance();
            helper.speakMessage(String.valueOf(keyTyped));
        }
    }
    
    private void showKeyDisplay(char keyTyped){
    	
    	typedCharacter = new ToolTipComponent();
    	
    	characterSize = new Dimension((int) (defaultSize.width * KeyboardAssistanceLookAndFeel.getInstance().getZoomFactor()), (int) (defaultSize.height * KeyboardAssistanceLookAndFeel.getInstance().getZoomFactor()));
    	
        typedCharacter.setText(String.valueOf(keyTyped));
        typedCharacter.setSize(defaultSize);
        typedCharacter.setPreferredSize(defaultSize);
        typedCharacter.setVisible(true);
        typedCharacter.setForeground(Color.BLACK);
        typedCharacter.setBackground(Color.WHITE);
        
        originalFont = typedCharacter.getFont();
        if(originalFont == null){
        	originalFont = new Font("Verdana", Font.PLAIN, 12);
        }
        Font font = new Font(null, keyTyped, keyTyped);
        Font newFont = originalFont.deriveFont((float)KeyboardAssistanceLookAndFeel.getInstance().getZoomFactor() * originalFont.getSize());
        typedCharacter.setFont(newFont);
        
        glass = (Container) curComponent.getRootPane().getGlassPane();
        glass.setLayout(null);
        glass.add(typedCharacter);
        glass.setComponentZOrder(typedCharacter, 0);
        
        typedCharacter.repaint();
        setPosition(glass, curComponent);
        GlassPaneManager.show(glass, typedCharacter);
        this.isDisplayOn = true;
        this.currentTimeStamp = System.currentTimeMillis();
    	
    }
    
    private void setPosition(Container glass, JComponent comp){
        Point componentPos = EasyAccessibilityLookAndFeel.getAbsolutePosition(comp);
        Dimension glassSize = glass.getSize();
        Dimension compSize = comp.getSize();
        Dimension actualToolTipSize = typedCharacter.getSize();
        
        int x = componentPos.x;
        int y = componentPos.y + compSize.height;
        boolean xOK = false;
        boolean yOK = false;
        //System.out.println(y + ", " + actualToolTipSize.height + ", " + (y + actualToolTipSize.height));
        if((x + actualToolTipSize.width) < glassSize.width){
            xOK = true;
        }
        if((y + actualToolTipSize.height) < glassSize.height){
            yOK = true;
        }
        if(xOK && yOK){
            typedCharacter.setBounds(x, y, defaultSize.width, defaultSize.height);
            return;
        }
        if(!xOK){
            x = x - actualToolTipSize.width;
        }
        if(!yOK){
            y = componentPos.y - actualToolTipSize.height;
            //System.out.println("Y Not OK");
            //System.out.println(y);
        }
        typedCharacter.setBounds(x, y, defaultSize.width, defaultSize.height);
        return;
        
    }
    
    private void hideDisplay(){
        System.out.println("hideDisplay" + isDisplayOn);
            if(glass != null && isDisplayOn){
	        glass.remove(typedCharacter);
	        GlassPaneManager.hide(glass, typedCharacter);
            }
            glass = null;
            isDisplayOn = false;
    }
    
    private class HideCharacterDisplay implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            if(glass != null && isDisplayOn && ((System.currentTimeMillis() - currentTimeStamp) > (KeyboardAssistanceLookAndFeel.getInstance().getCharacterDisplayTime() - 10))){
	        glass.remove(typedCharacter);
	        GlassPaneManager.hide(glass, typedCharacter);
                isDisplayOn = false;
                glass = null;
            }
        }
    }

	
}

